package com.dhcc.cdc.busi.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcOrigchapterEntity;
import com.dhcc.cdc.busi.service.ICdcOrigchapterService;
import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * 原始文档章
 * 
 * @ClassName: CdcOrigchapterController
 * @Description: TODO
 * @author jumper
 * @date 2021-03-15 10:20:07
 */
@Controller
@RequestMapping("${sys.backendPath}/cdc/cdcorigchapter")
public class CdcOrigchapterController extends BaseController {

	private String PREFIX = "/backend/cdc/cdcorigchapter/";

	@Autowired
	private ICdcOrigchapterService cdcOrigchapterService;
	

    /**
     * 跳转到原始文档章列表首页
     */
    @RequestMapping("")
    @Permission("cdcorigchapter:list")
    public String index() {
        return PREFIX + "cdcorigchapter.html";
    }
	
    /**
     * 跳转到新增原始文档章
     */
    @RequestMapping("/to_add")
    @Permission("cdcorigchapter:add")
    public String toAdd() {
        return PREFIX + "cdcorigchapter_add.html";
    }
	
	/**
     * 跳转到修改原始文档章
     */
    @RequestMapping("/to_update/{cdcOrigchapterId}")
    @Permission("cdcorigchapter:update")
    public String toUpdate(@PathVariable Long cdcOrigchapterId, Model model) {
        CdcOrigchapterEntity cdcOrigchapter = this.cdcOrigchapterService.selectById(cdcOrigchapterId);
        model.addAttribute("cdcOrigchapter",cdcOrigchapter);
        return PREFIX + "cdcorigchapter_edit.html";
    }

    /**
     * 获取原始文档章列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission("cdcorigchapter:list")
    public Object list(@RequestParam Map<String, Object> params) {
		Page<CdcOrigchapterEntity> page = new PageFactory<CdcOrigchapterEntity>().defaultPage();
        List<CdcOrigchapterEntity> result = this.cdcOrigchapterService.page(page,params);
        page.setRecords((List<CdcOrigchapterEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增原始文档章
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    @Permission("cdcorigchapter:add")
    @BussinessLog(value = "新增原始文档章")
    public Object add(CdcOrigchapterEntity cdcOrigchapter) {
        if (CommonUtil.isOneEmpty(cdcOrigchapter)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        cdcOrigchapter.insert();
        return R.ok();
    }

    /**
     * 删除原始文档章
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    @Permission("cdcorigchapter:delete")
    @BussinessLog(value = "删除原始文档章")
    public Object delete(@RequestParam Long cdcOrigchapterId) {
	
        this.cdcOrigchapterService.deleteById(cdcOrigchapterId);

        return R.ok();
    }

    /**
     * 修改原始文档章
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    @Permission("cdcorigchapter:update")
    @BussinessLog(value = "修改原始文档章")
    public Object update(CdcOrigchapterEntity cdcOrigchapter) {
        if (CommonUtil.isOneEmpty(cdcOrigchapter)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        CdcOrigchapterEntity old = this.cdcOrigchapterService.selectById(cdcOrigchapter.getId());

        old.setEpisodeId(cdcOrigchapter.getEpisodeId());
        old.setCode(cdcOrigchapter.getCode());
        old.setDescription(cdcOrigchapter.getDescription());
        old.setContent(cdcOrigchapter.getContent());
        old.setDocTypeId(cdcOrigchapter.getDocTypeId());
        old.setSourceId(cdcOrigchapter.getSourceId());
        old.setIsActive(cdcOrigchapter.getIsActive());
        old.updateAllColumnById();
        return R.ok();
    }
	
}
